package com.dsz.apibackend.exception;

import com.dsz.apibackend.common.BaseResponse;
import com.dsz.apibackend.common.ErrorCode;
import com.dsz.apibackend.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author dushangzhi
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 处理自定义事务异常
     */
    @ExceptionHandler(value = BusinessException.class)
    public BaseResponse<?> businessExceptionHandler(BusinessException e) {
        log.error("出现自定义异常：" + e.getMessage(), e);
        return ResultUtils.fail(e.getCode(), e.getMessage());
    }

    /**
     * 处理系统内部异常
     */
    @ExceptionHandler(value = RuntimeException.class)
    public BaseResponse<?> runtimeExceptionHandler(RuntimeException e) {
        log.error("出现系统内部异常：" + e.getMessage(), e);
        return ResultUtils.fail(ErrorCode.SYSTEM_ERROR);
    }
}
